package com.edou.member.controller.member;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.constant.SessionConstant;
import com.edou.base.controller.BaseController;
import com.edou.member.entity.MemberComment;
import com.edou.member.entity.MemberUser;
import com.edou.member.form.MemberCommentForm;
import com.edou.member.service.IMemberCommentService;
import com.edou.member.service.IMemberUserService;



/**
 * 会员评论
 * @creator     
 * @create-time 2016/4/18 16:26:28
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Controller("memberCommentController")
@Scope("prototype")
@RequestMapping("/member/comment")
public class MemberCommentController extends BaseController {

	@Resource(name = "memberCommentServiceImpl")
	private IMemberCommentService memberCommentService;
	
	@Resource(name = "memberUserServiceImpl")
	private IMemberUserService memberUserService;
	
	
	/**
	 * 删除当前会员的评论
	 * @return 
	 */
	@RequestMapping(value = "/delMemberUserIdAndObjectId.htm", method = RequestMethod.POST)
	public ModelAndView del(@ModelAttribute MemberCommentForm model,HttpServletRequest request,HttpServletResponse response,HttpSession session){
		try {
			MemberUser currentUser = (MemberUser)session.getAttribute(SessionConstant.CURRENT_MEMBER);
			if(currentUser != null){
				if(model.getItem().getObjectId() != null){
					memberCommentService.delByMemberIdAndObjectId(currentUser.getId(),model.getItem().getObjectId());
				}
				return ajaxJSON(Status.success,"删除成功");
			}else{
				return ajaxJSON(Status.success,"请先登录!");
			}
		} catch (Exception e) {
			String msg = "删除会员评论时发生异常：" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "删除失败，请稍后重试");
		}
	}
	
	/**
	 * 评论
	 * @param model
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/doComment.htm", method = RequestMethod.POST)
	public ModelAndView doComment(@ModelAttribute MemberCommentForm model,HttpServletRequest request,HttpSession session){
		try {
			MemberUser currentUser = (MemberUser)session.getAttribute(SessionConstant.CURRENT_MEMBER);
			MemberComment item = model.getItem();
			//验证是否评论过
			MemberComment memberComment = memberCommentService.getMemberCommentByObjectIdAndObjectType(item.getObjectId(),item.getObjectType());
			if(null != memberComment){
				return ajaxJSON(Status.error,"您已评论过，请勿重复评论");
			}
			
			item.setMemberUserId(currentUser.getId());
			item.setMemberName(currentUser.getName());
			memberCommentService.add(item);
			
			return ajaxJSON(Status.success,"评论成功");
		} catch (Exception e) {
			String msg = "评论出错" + e.getMessage();
			logger.error(msg,e);
			return ajaxJSON(Status.error, "评论失败,请稍后重试");
		}
	}
}